package com.wsecar.sharelogin.qq.core;

import android.app.Activity;
import android.os.Bundle;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.wsecar.sharelogin.qq.bean.QQShareParamInfo;
import com.wsecar.sharelogin.qq.bean.ShareInitConfig;
import com.wsecar.sharelogin.qq.listener.QQShareResultListener;
import com.wsecar.sharelogin.qq.utils.QQUtil;

public class QQShareImpl {

    private Activity mActivity;
    private ShareInitConfig shareInitConfig;
    private QQShareResultListener shareResultListener;
    private Tencent mTencent;

    /**
     * 分享QQ好友
     *
     * @activity activity
     * @listener listener
     */
    public void qqShare(Activity activity, ShareInitConfig shareInitConfig, QQShareParamInfo shareParamInfo, QQShareResultListener listener) {
        this.mActivity = activity;
        this.shareResultListener = listener;
        this.shareInitConfig = shareInitConfig;
        if (null == mTencent) {
            mTencent = Tencent.createInstance(shareInitConfig.getQqAppId(), activity);
        }
        Bundle params = transQQShareParamInfo(shareParamInfo);
        mTencent.shareToQQ(activity,params,qqShareListener);
    }

    private  IUiListener qqShareListener = new IUiListener() {
        @Override
        public void onCancel() {
            if (null != shareResultListener){
                shareResultListener.cancle();
            }
        }
        @Override
        public void onComplete(Object response) {
            if (null != shareResultListener){
                shareResultListener.shareSuccess(response);
            }
        }
        @Override
        public void onError(UiError e) {
            if (null != shareResultListener){
                shareResultListener.error(e.errorCode,e.errorMessage);
            }
        }
    };

    /**
     * 分享QQ空间
     *
     * @param activity
     * @param listener
     */
    public void qqShareQzone(Activity activity,ShareInitConfig shareInitConfig, QQShareParamInfo shareParamInfo, QQShareResultListener listener) {
        this.mActivity = activity;
        this.shareResultListener = listener;
        this.shareInitConfig = shareInitConfig;
        if (null == mTencent) {
            mTencent = Tencent.createInstance(shareInitConfig.getQqAppId(), activity);
        }
        Bundle params = transQQShareParamInfo(shareParamInfo);
        if (null == params){
            listener.error(-1,"分享失败");
        }
        mTencent.shareToQzone(activity,params,qqShareListener);
    }

    /**
     * 将分享参数转成Build
     * @param shareParamInfo
     * @return
     */
    private Bundle  transQQShareParamInfo(QQShareParamInfo shareParamInfo){
        Bundle params = null;
        if (null == shareParamInfo){
            return params;
        }
        params = new Bundle();
        int mExtarFlag = shareParamInfo.getmExtarFlag();
        int shareType = shareParamInfo.getShareType();
        if (shareType != QQShare.SHARE_TO_QQ_TYPE_IMAGE) {
            params.putString(QQShare.SHARE_TO_QQ_TITLE, shareParamInfo.getTitle());
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareParamInfo.getTargetUrl());
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareParamInfo.getSummary());
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareParamInfo.getImageUrl());
        } else {
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, shareParamInfo.getImageUrl());
        }
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, shareParamInfo.getAppName());
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, shareType);
        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, mExtarFlag);
        if (shareType == QQShare.SHARE_TO_QQ_TYPE_AUDIO) {
            params.putString(QQShare.SHARE_TO_QQ_AUDIO_URL, shareParamInfo.getAudioUrl());
        }
        if ((mExtarFlag & QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN) != 0) {
            QQUtil.toastMessage(mActivity,"在好友选择列表会自动打开分享到qzone的弹窗~~~");
        } else if ((mExtarFlag & QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE) != 0) {
           QQUtil.toastMessage(mActivity,"在好友选择列表隐藏了qzone分享选项~~~");
        }
        // 小程序
        if (shareType == QQShare.SHARE_TO_QQ_MINI_PROGRAM) {
            params  = new Bundle();
            params.putString(QQShare.SHARE_TO_QQ_TITLE, shareParamInfo.getTitle());
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareParamInfo.getSummary());
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareParamInfo.getTargetUrl());
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareParamInfo.getImageUrl());
            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_APPID, shareParamInfo.getMiniProgramAppid());
            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_PATH,shareParamInfo.getMiniProgramPath());
            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_TYPE,shareParamInfo.getMiniProgramType());
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, shareType);
        }
        if (QQShare.SHARE_TO_QQ_TYPE_IMAGE != shareType) {
            /*QQ分享增加ARK*/
            String arkStr = shareParamInfo.getArkInfo();
            params.putString(QQShare.SHARE_TO_QQ_ARK_INFO, arkStr);
            /*QQ分享增加ARK end*/
        }
        return params;
    }

    public void  destoryShareTencent(){
        if (null != mTencent) {
            mTencent.releaseResource();
        }
    }
}
